import OutputDataParser;

import bsddb;
import md5;
import os;

DEFAULT_CACHE_SIZE = 2 ** 29;

class UrlDb:
    def __init__(self, path):
        self.db = bsddb.btopen(path, cachesize = DEFAULT_CACHE_SIZE);
        self.path = path;
    
    def loadFromPostingFile(self, postingFilePath):
        if not os.path.exists(postingFilePath):
            return -1;
        postingFile = OutputDataParser.PostingFile();
        if postingFile.open(postingFilePath) < 0:
            return -1;
        return self.loadFromFile(postingFile);
        
    def loadFromProfileFile(self, profileFilePath):
        if not os.path.exists(profileFilePath):
            return -1;
        profileFile = OutputDataParser.ProfileFile();
        if profileFile.open(profileFilePath) < 0:
            return -1;
        return self.loadFromFile(profileFile);
        
    def loadFromFile(self, dataFile):
        self.db.close();
        if(os.path.exists(self.path)):
            os.remove(self.path);
        self.db = bsddb.btopen(self.path, cachesize = DEFAULT_CACHE_SIZE);
        while dataFile.next() >= 0:
            element = dataFile.getCurrElement();
            url = element.url;
            self.add(url);
           
    def add(self, url):
        digest = md5.md5(url).digest();
        self.db[digest] = '';
        
    def contains(self, url):
        digest = md5.md5(url).digest();
        if self.db.has_key(digest):
            return 1;
        else:
            return 0;
        
if __name__ == '__main__':
    db = UrlDb('tmp.db');
    db.add('test1');
    db.add('test2');
    db.add('test3');
    print db.contains('test4');
    print db.contains('test2');
    os.remove('tmp.db');        
